package com.example.agricultural_big_data.service.serviceimpl;

import com.alibaba.fastjson.JSONObject;
import com.example.agricultural_big_data.mapper.PlantMapper;
import com.example.agricultural_big_data.pojo.*;
import com.example.agricultural_big_data.service.PlantService;
import com.example.agricultural_big_data.util.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.LinkedList;
import java.util.List;

/**
 * @author xiao yang
 * @Description:
 * @date 2021/2/2821:32
 */
@Service
public class PlantServiceImpl implements PlantService {
    @Autowired
    PlantMapper plantMapper;

    @Autowired
    Result result;

    @Override
    public List<Plantation> getPlantationInfo(String userId) {
        List<Plantation> list = new ArrayList<>();
        // 1.判断user是否为超级管理员
        if (userId.equalsIgnoreCase("8e883e8192e643b4a8d4f6d72afa1edd")){
            list = plantMapper.getPlantationList();
        }else{
            // 根据关联表查询出当前管理员所对应的所有种植园id
           List<UserRelation> relations = plantMapper.getRelationListByUserId(userId);
           // 根据种植园id获取种植园信息
            for (int i = 0; i < relations.size(); i++) {
                Plantation plantation = plantMapper.getPlantationById(relations.get(i).getPlantationId());
                list.add(plantation);
            }
        }
        return list;
    }

    @Override
    public List<Plantation> getPlantation() {
        return plantMapper.getPlantationList();
    }


    @Override
    public JSONObject getHumidityByid(String pl_id) {
        JSONObject jsonObject;
        List<PlantHumidity> humidities=plantMapper.getHumidityByid(pl_id);
        for (PlantHumidity humidity: humidities) {
             int pl_humidity=humidity.getHumidity();
             humidity.setSd(pl_humidity/100.f);
        }
        jsonObject=result.responseResult(ResultCode.success.getStatusCode(),ResultCode.success.getMsg(),humidities);
        return jsonObject;
    }

    @Override
    public JSONObject getKByid(String pl_id) {
        JSONObject jsonObject;
        List<PlantK>plantKS=plantMapper.getKByid(pl_id);
        jsonObject=result.responseResult(ResultCode.success.getStatusCode(),ResultCode.success.getMsg(),plantKS);
        return jsonObject;
    }

    @Override
    public JSONObject getLightByid(String pl_id) {
        JSONObject jsonObject;
        List<PlantLight>plantLights=plantMapper.getLightByid(pl_id);
        jsonObject=result.responseResult(ResultCode.success.getStatusCode(),ResultCode.success.getMsg(),plantLights);
        return jsonObject;
    }

    @Override
    public JSONObject getNByid(String pl_id) {
        JSONObject jsonObject;
        List<PlantN>plantNS=plantMapper.getNByid(pl_id);
        jsonObject=result.responseResult(ResultCode.success.getStatusCode(),ResultCode.success.getMsg(),plantNS);
        return jsonObject;
    }

    @Override
    public JSONObject getPByid(String pl_id) {
        JSONObject jsonObject;
        List<PlantP>plantPS=plantMapper.getPByid(pl_id);
        jsonObject=result.responseResult(ResultCode.success.getStatusCode(),ResultCode.success.getMsg(),plantPS);
        return jsonObject;
    }

    @Override
    public JSONObject getPhByid(String pl_id) {
        JSONObject jsonObject;
        List<PlantPh>plantPhs=plantMapper.getPhByid(pl_id);
        jsonObject=result.responseResult(ResultCode.success.getStatusCode(),ResultCode.success.getMsg(),plantPhs);
        return jsonObject;
    }

    @Override
    public JSONObject getTemperatureByid(String pl_id)
    {
        JSONObject jsonObject;
        List<PlantTemperature>plantTemperatures=plantMapper.getTemperatureByid(pl_id);
        jsonObject=result.responseResult(ResultCode.success.getStatusCode(),ResultCode.success.getMsg(),plantTemperatures);
        return jsonObject;
    }

    @Override
    public JSONObject getWindByid(String pl_id) {
        JSONObject jsonObject;
        List<PlantWind>plantWinds=plantMapper.getWindByid(pl_id);
        jsonObject=result.responseResult(ResultCode.success.getStatusCode(),ResultCode.success.getMsg(),plantWinds);
        return jsonObject;
    }

    @Override
    public JSONObject getPlantNPK(String pl_id) {
        JSONObject jsonObject;
        List<PlantN> plantNS=plantMapper.getNByid(pl_id);
        List<PlantP> plantPS=plantMapper.getPByid(pl_id);
        List<PlantK> plantKS=plantMapper.getKByid(pl_id);
        PlantNPK plantNPK=new PlantNPK(plantNS,plantPS,plantKS);
        jsonObject=result.responseResult(ResultCode.success.getStatusCode(),ResultCode.success.getMsg(),plantNPK);
        return jsonObject;
    }

    @Override
    public JSONObject getHumidityByidAndMonth(String pl_id) {
        JSONObject jsonObject;
        List<PlantHumidity> humidities=plantMapper.getHumidityByidAndMonth(pl_id);
        int sum=0;
        int flag=0;
        for (PlantHumidity humidity: humidities) {
            sum=sum+humidity.getHumidity();
            flag=flag+1;
        }
        int rs=sum/flag;
        jsonObject=result.responseResult(ResultCode.success.getStatusCode(),ResultCode.success.getMsg(),rs);
        return jsonObject;
    }

    @Override
    public JSONObject getKByidAndMonth(String pl_id) {
        JSONObject jsonObject;
        List<PlantK> ks=plantMapper.getKByidAndMonth(pl_id);
        int sum=0;
        int flag=0;
        for (PlantK K: ks) {
            sum=sum+K.getK();
            flag=flag+1;
        }
        int rs=sum/flag;
        jsonObject=result.responseResult(ResultCode.success.getStatusCode(),ResultCode.success.getMsg(),rs);
        return jsonObject;
    }

    @Override
    public JSONObject getLightByidAndMonth(String pl_id) {
        JSONObject jsonObject;
        List<PlantLight> lights=plantMapper.getLightByidAndMonth(pl_id);
        int sum=0;
        int flag=0;
        for (PlantLight light: lights) {
            sum=sum+light.getLight();
            flag=flag+1;
        }
        int rs=sum/flag;
        jsonObject=result.responseResult(ResultCode.success.getStatusCode(),ResultCode.success.getMsg(),rs);
        return jsonObject;
    }

    @Override
    public JSONObject getNByidAndMonth(String pl_id) {
        JSONObject jsonObject;
        List<PlantN> ns=plantMapper.getNByidAndMonth(pl_id);
        int sum=0;
        int flag=0;
        for (PlantN n: ns) {
            sum=sum+n.getN();
            flag=flag+1;
        }
        int rs=sum/flag;
        jsonObject=result.responseResult(ResultCode.success.getStatusCode(),ResultCode.success.getMsg(),rs);
        return jsonObject;
    }

    @Override
    public JSONObject getPByidAndMonth(String pl_id) {
        JSONObject jsonObject;
        List<PlantP> ps=plantMapper.getPByidAndMonth(pl_id);
        int sum=0;
        int flag=0;
        for (PlantP p: ps) {
            sum=sum+p.getP();
            flag=flag+1;
        }
        int rs=sum/flag;
        jsonObject=result.responseResult(ResultCode.success.getStatusCode(),ResultCode.success.getMsg(),rs);
        return jsonObject;
    }

    @Override
    public JSONObject getPhByidAndMonth(String pl_id) {
        JSONObject jsonObject;
        List<PlantPh> phs=plantMapper.getPhByidAndMonth(pl_id);
        int sum=0;
        int flag=0;
        for (PlantPh ph: phs) {
            sum=sum+ph.getPh();
            flag=flag+1;
        }
        int rs=sum/flag;
        jsonObject=result.responseResult(ResultCode.success.getStatusCode(),ResultCode.success.getMsg(),rs);
        return jsonObject;
    }

    @Override
    public JSONObject getTemperatureByidAndMonth(String pl_id) {
        JSONObject jsonObject;
        List<PlantTemperature> temperatures=plantMapper.getTemperatureByidAndMonth(pl_id);
        int sum=0;
        int flag=0;
        for (PlantTemperature temperature: temperatures) {
            sum=sum+temperature.getTemperature();
            flag=flag+1;
        }
        int rs=sum/flag;
        jsonObject=result.responseResult(ResultCode.success.getStatusCode(),ResultCode.success.getMsg(),rs);
        return jsonObject;
    }

    @Override
    public JSONObject getWindByidAndMonth(String pl_id) {
        JSONObject jsonObject;
        List<PlantWind> winds=plantMapper.getWindByidAndMonth(pl_id);
        int sum=0;
        int flag=0;
        for (PlantWind wind: winds) {
            sum=sum+wind.getWind();
            flag=flag+1;
        }
        int rs=sum/flag;
        jsonObject=result.responseResult(ResultCode.success.getStatusCode(),ResultCode.success.getMsg(),rs);
        return jsonObject;
    }

    @Override
    public JSONObject getPlantsAvGByMarketId(String p_id) {
        JSONObject jsonObject;
        List<Plant> plants=plantMapper.getPlantByMarketId(p_id);
        List<PlantAvg> plantAvgs=new LinkedList<>();
        for (Plant p:plants) {
            PlantAvg plantAvg=new PlantAvg();
            String pl_id=p.getPl_id();
            String c_id=p.getC_id();
            Crop crop=plantMapper.getNameByCid(c_id);

            List<PlantHumidity> humidities=plantMapper.getHumidityByidAndMonth(pl_id);
            int sum=0;
            int flag=0;
            for (PlantHumidity humidity: humidities) {
                sum=sum+humidity.getHumidity();
                flag=flag+1;
            }
            int humidityavg=sum/flag;
            plantAvg.setHumidity(humidityavg);
            // K的平均值
            List<PlantK> ks=plantMapper.getKByidAndMonth(pl_id);
             sum=0;
             flag=0;
            for (PlantK K: ks) {
                sum=sum+K.getK();
                flag=flag+1;
            }
            int Kavg=sum/flag;
            plantAvg.setK(Kavg);
           //光照平均值
            List<PlantLight> lights=plantMapper.getLightByidAndMonth(pl_id);
            sum=0;
            flag=0;
            for (PlantLight light: lights) {
                sum=sum+light.getLight();
                flag=flag+1;
            }
            int lightavg=sum/flag;
            plantAvg.setLight(lightavg);
            //获取n的平均值
            List<PlantN> ns=plantMapper.getNByidAndMonth(pl_id);
            sum=0;
            flag=0;
            for (PlantN n: ns) {
                sum=sum+n.getN();
                flag=flag+1;
            }
            int Navg=sum/flag;
            plantAvg.setN(Navg);
            //P平均值
            List<PlantP> ps=plantMapper.getPByidAndMonth(pl_id);
            sum=0;
            flag=0;
            for (PlantP p1: ps) {
                sum=sum+p1.getP();
                flag=flag+1;
            }
            int Pavg=sum/flag;
            plantAvg.setP(Pavg);
            //设置ph平均值
            List<PlantPh> phs=plantMapper.getPhByidAndMonth(pl_id);
            sum=0;
            flag=0;
            for (PlantPh ph: phs) {
                sum=sum+ph.getPh();
                flag=flag+1;
            }
            int Phavg=sum/flag;
            plantAvg.setPh(Phavg);
            //设置温度
            List<PlantTemperature> temperatures=plantMapper.getTemperatureByidAndMonth(pl_id);
            sum=0;
            flag=0;
            for (PlantTemperature temperature: temperatures) {
                sum=sum+temperature.getTemperature();
                flag=flag+1;
            }
            int temperatureavg=sum/flag;
            plantAvg.setTemperature(temperatureavg);
            //设置风速
            sum=0;
            flag=0;
            List<PlantWind> winds=plantMapper.getWindByidAndMonth(pl_id);
            for (PlantWind wind: winds) {
                sum=sum+wind.getWind();
                flag=flag+1;
            }
            int Windavg=sum/flag;
            plantAvg.setWind(Windavg);
            plantAvg.setPl_id(p.getPl_id());

            plantAvgs.add(plantAvg);

        }
        jsonObject=result.responseResult(ResultCode.success.getStatusCode(),ResultCode.success.getMsg(),plantAvgs);
        return jsonObject;
    }




}
